import java.util.*;
public class Brokerage implements Login
{
	public TreeMap<String, Trader> traders;
	private TreeSet<Trader> active;
	private StockExchange exchange;
	
	public Brokerage(StockExchange exchange)
	{
		this.exchange = exchange;
		traders = new TreeMap<String, Trader>();
		active = new TreeSet<Trader>();
	}
	public int addUser(String name, String password)
	{
		if (name.length() < 4 || name.length() > 10)
			return -1;
		else if (password.length() < 2 || password.length() > 10)
			return -2;
		else if (traders.keySet().contains(name))
			return -3;
		else
		{
			traders.put(name, new Trader(this, name, password));
			return 0;
		}
	}
	public int login(String name, String password)
	{
		if (!traders.keySet().contains(name))
			return -1;
		else if (!traders.get(name).getPassword().equals(password))
			return -2;
		else if (active.contains(traders.get(name)))
			return -3;
		Trader trader = traders.get(name);
		active.add(traders.get(name));
		trader.openWindow();
		if (!trader.hasMessages())
			trader.recieveMessage("Welcome to SafeTrade");
		return 0;
	}
	public void logout(Trader trader)
	{
		if (active.contains(trader))
			active.remove(trader);
	}
	public void getQuote(String symbol, Trader trader)
	{
		trader.recieveMessage(exchange.getQuote(symbol));
	}
	public void placeOrder(TradeOrder order)
	{
		exchange.placeOrder(order);
		
	}
}

